package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.student.dms.databinding.FragmentStudentHomeBinding;
import com.student.dms.databinding.ItemRecyerviewBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StudentHomeFragment extends BaseFragment<FragmentStudentHomeBinding> {

    @Override
    public void setViewBinding() {
        mViewBinding = FragmentStudentHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    public void init() {

        BaseQuickAdapter baseQuickAdapter = new BaseQuickAdapter<ItemData, VH>() {
            @NonNull
            @Override
            protected VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                VH vh = new VH(ItemRecyerviewBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull VH vh, int position, @Nullable ItemData itemData) {
                vh.itemViewBidnng.textView.setText(itemData.getName());
                vh.itemViewBidnng.imageView.setImageResource(itemData.getIcon());
                vh.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (vh.getAbsoluteAdapterPosition()) {
                            case 0:
                                startActivity(new Intent(requireActivity(), AnnouncementPushActivity.class));
                                break;
                            case 1:
                                startActivity(new Intent(requireActivity(), FixReportActivity.class));
                                break;
                            case 2:
                                startActivity(new Intent(requireActivity(), ActivityDayScore.class));
                                break;
                            case 3:
                                startActivity(new Intent(requireActivity(), PaymentActivity.class));
                                break;
                            case 4:
                                startActivity(new Intent(requireActivity(), ActivityPunchInAndOut.class));
                                break;
                        }

                    }
                });
            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        mViewBinding.recyclerView.setAdapter(build.getAdapter());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireActivity(), 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == build.getAdapter().getItemCount() - 1) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        mViewBinding.recyclerView.setLayoutManager(gridLayoutManager);

        ArrayList<ItemData> list = getItemData();
        baseQuickAdapter.submitList(list);
    }

    private static @NonNull ArrayList<ItemData> getItemData() {
        ArrayList<ItemData> list = new ArrayList<>();
        // 公告信息
        ItemData ggxx = new ItemData();
        ggxx.setIcon(R.mipmap.renwu);
        ggxx.setName("公告\n展示");
        list.add(ggxx);
        // 保修记录
        ItemData bxsb = new ItemData();
        bxsb.setName("报修\n上报");
        bxsb.setIcon(R.mipmap.ic_repairs);
        list.add(bxsb);

        // 评分查询
        ItemData pfcx = new ItemData();
        pfcx.setIcon(R.mipmap.ic_score);
        pfcx.setName("评分\n查询");
        list.add(pfcx);


      /* // 校园帮助
        ItemData xybz = new ItemData();
        xybz.setName("校园\n帮助");
        list.add(xybz);*/

        // 水电缴费
        ItemData sdjf = new ItemData();
        sdjf.setName("水电\n缴费");
        sdjf.setIcon(R.mipmap.ic_pay);
        list.add(sdjf);


    /*    // 体温上报
        ItemData twsb = new ItemData();
        twsb.setName("体温\n上报");
        list.add(twsb);*/


        // 出入打卡
        ItemData crdk = new ItemData();
        crdk.setName("出入打卡");
        crdk.setIcon(R.mipmap.ic_come);
        ggxx.setType(-1);
        list.add(crdk);
        return list;
    }


    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemRecyerviewBinding itemViewBidnng;

        public VH(@NonNull ItemRecyerviewBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }
}
